package org.jf.tool.fx.jftoolfx.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author jifeng_zhang
 * @date 2025/8/18 下午8:19
 */
public class SettingsUtil {

    private static final String SETTINGS_PATH = System.getProperty("user.dir") + File.separator + "settings.json";

    public static String getTheme() {
        File file = new File(SETTINGS_PATH);
        if (!file.exists()) {
            return "PrimerLight";
        }
        try {
            JSONObject json = JSON.parseObject(Files.readAllBytes(Paths.get(SETTINGS_PATH)));
            return json.getString("theme");
        } catch (Exception e) {
            return "PrimerLight";
        }
    }

    public static boolean saveSettings(Settings settings) {
        File file = new File(SETTINGS_PATH);
        //写入文件
        try (FileWriter fileWriter = new FileWriter(file)) {
            JSONObject json = new JSONObject();
            json.put("theme", settings.getTheme());
            fileWriter.write(json.toString());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static class Settings {

        public String theme;

        public String getTheme() {
            return theme;
        }

        public void setTheme(String theme) {
            this.theme = theme;
        }

    }
}
